<?php

/**
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace AdminModule\Forms;

use Nette\Application\UI\Form,
    Nette\Environment,
    Nette\Security\AuthenticationException;

class LoginForm extends Form
{
    public function __construct($parent, $name)
    {
        parent::__construct($parent, $name);

        $this->addProtection('Prosím odošlite prihlasovacie údaje znova (vypršala platnosť bezpečnostného tokenu).');

        $this->addText('login', 'Už. jméno:')
            ->addRule(Form::FILLED, 'Prosím zadejte uživatelské jméno.')
                ->getControlPrototype()->class('login-inp');

        $this->addPassword('password', 'Heslo:')
            ->addRule(Form::FILLED, 'Prosím zadejte heslo.')
            ->getControlPrototype()->class('login-inp');

        $this->addSubmit('send', 'Přihlásit')
                ->getControlPrototype()->class('submit-login');
        $this->onSuccess[] = array($this, 'formSubmited');
    }

    public function formSubmited($form)
    {
        try {
            $user = $this->getPresenter()->getUser();
            $user->login($form['login']->value, $form['password']->value);

            $this->getPresenter()->getApplication()->restoreRequest($this->getPresenter()->backlink);
            $this->getPresenter()->redirect('Dashboard:default');
        }
        catch (AuthenticationException $e) {
            $form->addError($e->getMessage());
        }
    }
}